/**
 * 
 */
package com.google.code.cisx.gwt.client;

import com.google.code.cisx.gwt.client.components.NavigationComponent;
import com.google.code.cisx.gwt.client.components.login.LoginComponent;
import com.google.code.cisx.gwt.client.components.login.LoginEvent;
import com.google.code.cisx.gwt.client.components.login.LoginEventListener;
import com.google.code.cisx.gwt.client.components.login.LoginEvent.LoginEventType;
import com.google.code.cisx.gwt.client.event.EventManager;
import com.google.code.cisx.gwt.client.layouts.LayoutTemplate;
import com.google.code.cisx.gwt.client.layouts.MainLayoutTemplate;
import com.google.code.cisx.gwt.client.layouts.SingleContentLayoutTemplate;
import com.google.code.cisx.gwt.client.session.Session;
import com.google.code.cisx.gwt.client.session.SessionEvent;
import com.google.code.cisx.gwt.client.session.SessionTracker;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * The main application page that has a layout assoicated. The layout is then used to layout various components.
 * 
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @version $Id: ApplicationHomePage.java 262 2008-12-01 09:11:22Z rahul.thakur.xdev $
 * @since 1.0
 */
public class ApplicationHomePage extends Composite implements EntryPoint, LoginEventListener<LoginEvent> {

  private LayoutTemplate currentLayout = null;

  // @Override
  public void onModuleLoad() {
    // build layout
    LayoutTemplate tpl = null;
    // FIXME: Every refresh of HomePage seems to create a
    // new instance of SessionTracker, thereby losing the earlier
    // session in progress.
    // Need to come back to this!
    Session session = SessionTracker.getInstance().getSession();
    // use appropriate layout template based on session state.
    if (null != session && session.isActive()) {
      GWT.log(session.toString(), null);
      tpl = getMainLayoutTemplate();
    } else {
      GWT.log("Session is NULL", null);
      tpl = getSingleLayoutTemplate();
    }

    setCurrentLayout(tpl);

    EventManager.getInstance().addEventListener(LoginEventType.LOGIN, this);
    EventManager.getInstance().addEventListener(LoginEventType.LOGOUT, this);
    EventManager.getInstance().addEventListener(LoginEventType.TIMEOUT, this);
  }

  /**
   * @return
   */
  private SingleContentLayoutTemplate getSingleLayoutTemplate() {
    SingleContentLayoutTemplate tpl = new SingleContentLayoutTemplate();
    tpl.setComponent("content", new LoginComponent());
    return tpl;
  }

  /**
   * Helper that does <b>two</b> things:
   * <ul>
   * <li>Ensures that {@link LayoutTemplate#render()} is called.</li>
   * <li>Removes the current layout from the Root container before adding the passed in {@link LayoutTemplate}.</li>
   * </ul>
   * 
   * @param tpl
   *          LayoutTemplate to set
   */
  private void setCurrentLayout(LayoutTemplate tpl) {
    tpl.render();
    if (null != currentLayout)
      this.currentLayout.removeFromParent();

    RootPanel.get().add(tpl);
    this.currentLayout = tpl;
  }

  // @Override
  public void onEvent(LoginEvent event) {
    if (event.getType() == LoginEvent.LoginEventType.LOGIN) {
      // create a session
      Session newSession = Session.newSession();
      SessionTracker.getInstance().bindSession(newSession);
      EventManager.getInstance().dispatchEvent(new SessionEvent(this, SessionEvent.SessionEventType.CREATED));

      // We've logged in so change the page contents now!
      LayoutTemplate tpl = getMainLayoutTemplate();
      setCurrentLayout(tpl);
    } else {
      // Invalidate session.
      SessionTracker.getInstance().unbindSession();
      if (event.getType() == LoginEvent.LoginEventType.TIMEOUT)
        EventManager.getInstance().dispatchEvent(new SessionEvent(this, SessionEvent.SessionEventType.TIMED_OUT));
      else
        EventManager.getInstance().dispatchEvent(new SessionEvent(this, SessionEvent.SessionEventType.TERMINATED));
    }
  }

  /**
   * @return
   */
  private MainLayoutTemplate getMainLayoutTemplate() {
    MainLayoutTemplate tpl = new MainLayoutTemplate();
    tpl.setComponent(MainLayoutTemplate.PLACEHOLDER_HEADER, new HTML("Continuous Integration Server"));
    tpl.setComponent(MainLayoutTemplate.PLACEHOLDER_NAVIGATION, new NavigationComponent());
    tpl.setComponent(MainLayoutTemplate.PLACEHOLDER_CONTENT, new HTML(" ---- content goes here ----- "));
    tpl.setComponent(MainLayoutTemplate.PLACEHOLDER_FOOTER, new HTML("Copyright Rahul Thakur 2009"));
    return tpl;
  }

}
